import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:rename_exif/main_controller.dart';
import 'package:get/get.dart';
import 'package:tuple/tuple.dart';
import 'package:path/path.dart' as p;

class FilesView extends StatelessWidget {
  const FilesView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var c = Get.find<MainController>();
    return Container(
        alignment: Alignment.topCenter,
        // color: Colors.grey[100],
        // padding: const EdgeInsets.all(2),
        child: Obx(() =>DataTable2(
          // key: UniqueKey(),
          columnSpacing: 8,
          horizontalMargin: 8,
          showCheckboxColumn: true,
          showBottomBorder: true,
          minWidth: 10,
          headingRowColor: MaterialStateProperty.all(Colors.cyan[800]),
          headingRowHeight: 30,
          headingTextStyle: Theme.of(context).textTheme.subtitle1,
          dataTextStyle:  Theme.of(context).textTheme.bodyText1,
          // dataRowColor: MaterialStateProperty.all(Colors.lightGreen[50]),
          dataRowHeight: 30,
          smRatio: .8,
          lmRatio: 3.5,
          // scrollController: c.fileListSC,
          scrollController: ScrollController(),
          sortColumnIndex: c.currentSortColumn.value,
          sortAscending: c.isSortAscending.value,
          columns: [
            const DataColumn2(
              label:  Align(alignment: Alignment.center, child:Text('ID')),
              size: ColumnSize.M,
              numeric: true,
            ),
            DataColumn2(
              label: const Text('旧文件名'),
              size: ColumnSize.L,
              onSort: (columnIndex, _) => c.onFileListSortOld(columnIndex),
            ),
            DataColumn2(
              label: const Text('新文件名'),
              size: ColumnSize.L,
              onSort: (columnIndex, _) => c.onFileListSortNew(columnIndex),
            ),
            const DataColumn2(
              label: Align(alignment: Alignment.center, child:Text('操作')),
              size: ColumnSize.S,
            ),
          ],
          rows: _dataRows(context, c.fileList.value),
        )
    ));
  }

  _dataRows(BuildContext context, List<Tuple5<int, FileSystemEntity, String, bool, int>> list) {
    int index = 0;
    var c = Get.find<MainController>();
    return list.map((d){
      return DataRow2(cells: [
        DataCell(Align(alignment: Alignment.center, child:Text("${index++}"))),
        DataCell(Tooltip(message: d.item2.path, child: Text(p.basename(d.item2.path), overflow: TextOverflow.ellipsis))),
        DataCell(Tooltip(message: d.item3, child: Text(p.basename(d.item3), overflow: TextOverflow.ellipsis))),
        DataCell(Align(alignment: Alignment.center, child: _action(d.item1, d.item5)))],
        // onSelectChanged: (v) => print("onSelected"),
        // selected: false,
        color: d.item4 ? MaterialStateProperty.all(Theme.of(context).hintColor) : MaterialStateProperty.all(Theme.of(context).hoverColor),
        onTap: () => c.onTapDir(d.item1),
      );
    }).toList();
  }

  Widget _action(int index, int res) {
    var c = Get.find<MainController>();
    if (res == 1) {
      return const Icon(Icons.verified_outlined, color: Colors.green);
    } else if (res == 2) {
      return const Icon(Icons.error_outline, color: Colors.red);
    }

    return TextButton(
        onPressed: () => c.delFile(index),
        child: const Text("移除", maxLines: 1, style: TextStyle(fontSize: 14),)
    );
  }
}
